﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace HoteCode.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PhotoController : ControllerBase
    {
        [HttpPost]
        public IActionResult GetPhoto(IFormFile file)
        {
            try
            {
                string newfile = Directory.GetCurrentDirectory() + @"\wwwroot\imgs\" + file.FileName;
                var t = file.ContentType;
                var ts = new List<string>
                {
                    "imgs/jpg",
                    "imgs/jpeg",
                    "imgs/png",
                   // "imgs/webp",
                };
                if (!ts.Contains(t))
                {
                    Ok("不是图片");
                }
                var m = 1024 * 1024 * 2;
                if(file.Length>m)
                {
                    return Ok("图片不能大于2M");
                }
                using (FileStream fs = new FileStream(newfile, FileMode.Create))
                {
                    file.CopyTo(fs);
                    fs.Close();
                }
                return Ok("https://localhost:7289/imgs/"+file.FileName);
            }
            catch (Exception)
            {

                throw;
            }

        }
    }
}
